<?php
//This is going to be an RSS feed
header("Content-Type: application/xml; charset=ISO-8859-1");

//for now, should come from a more dynamic source.
$localAffiliates = array("KOLNDT"=>"CBS", "KXVODT"=>"CW", "WOWTDT"=>"NBC", "KETVDT"=>"ABC", "KLKNDT"=>"ABC", "KLKNSD"=>"RTN", "KPTMDT"=>"FOX",);

class RSS 
{
	public function RSS()
	{
		
	}
	
	public function getFeed()
	{
		return $this->getDetails() . $this->getItems();
	}
	
	private function stationAffiliate($localStation) //we want the parent station data (ABC, CBS, FOX, etc...)
	{
		global $localAffiliates;
		$localAffiliate = $localAffiliates[$localStation];
		if ($localAffiliate) {
			return $localAffiliate;
		} else {
			return "";
		}
	}
	
	private function getDetails()
	{
		$details = '<?xml version="1.0" encoding="ISO-8859-1"?>
                <rss version="2.0" xmlns:boxee="http://boxee.tv/rss/spec" xmlns:media="http://search.yahoo.com/mrss/">
                    <channel>
                        <title>Local TV</title>
                        <link>http://'. $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] .'</link>
                        <description>All your local televesion goodness.</description>  
                        <image>
                            <title></title>
                            <url>http://'. $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] .'</url>
                            <link>http://'. $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] .'</link>
                            <width></width>
                            <height></height>
                        </image>
                        ';
        return $details;
	}
	
	private function array_find($needle, $haystack, $search_keys = false) {
		if(!is_array($haystack)) return false;
        foreach($haystack as $key=>$value) {
            $what = ($search_keys) ? $key : $value;
            if(strpos($what, $needle)!==false) return $key;
        }
        return false;
	}
	
	private function videoWidth($station) //if the source says DT, assume 720p. Otherwise 480p
	{
	   if(substr($station, 4, 2) === "DT"){
	       return '1280';
	   }
	   return '720';
	}
	
    private function videoHeight($station) //if the source says DT, assume 720p. Otherwise 480p
    {
       if(substr($station, 4, 2) === "DT"){
           return '720';
       }
       return '480';
    }
	private function getItems()
	{
	   //Get all the stream files (eg. 11.1 KOLNDT.strm)
        $strmFiles=glob('streams/*.strm')?glob('streams/*.strm'):array();
        
        //parse out channel and network
        $stations = array(); //array to hold channel and network
        foreach ($strmFiles as $strmFile) {
            preg_match("/[0-9]+((?:\.[0-9]))?/", $strmFile, $channels); //based on number
            preg_match("/[A-Z]+/", $strmFile, $networks); //based on networks
            
            $stations[$channels[0]] = $networks[0];
        }
        ksort($stations, SORT_NUMERIC);
        $items = '';
        foreach ($stations as $key => $value)
		{
			$items .= '<item>
			     <title>'.$key.'</title>
			     <link>'.$value.'</link>
			     <description>'.$value.'</description>
			     <boxee:property name="station">'.$this->stationAffiliate($value).'</boxee:property>
			     <boxee:property name="Image0">http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] . 'images/stations/' . $this->stationAffiliate($value).'.png</boxee:property>
			     <media:thumbnail url="http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] . 'images/stations/' . $this->stationAffiliate($value).'.png"></media:thumbnail>
			     <media:content url="http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] . $strmFiles[$this->array_find($key.' '.$value, $strmFiles)] .'" type="video/mpeg" height="'.$this->videoHeight($value).'" width="'.$this->videoWidth($value).'" lang="en-us" />
			  </item>
			  ';
		}
		$items .= '</channel>
		      </rss>';
		return $items;
	}
	
	private function parseSource()
	{
	   return $station;
	}
}

$rss = new RSS();
echo $rss->getFeed();